﻿using System;
using SharpKit.JavaScript;

namespace SharpKit.Onx
{
    /// <summary>
    /// Network state collector for 3G and WiFi.
    /// https://www.onx.ms/#apiPage/network
    /// </summary>
    [JsType(Mode = JsMode.Prototype, Export = false)]
    public class Network : Emitter
    {
        //No way to new this up
        private Network()
        { }

        #region Actions (Methods)
        /// <summary>
        /// Starts an asynchronous WiFi scan.
        /// </summary>
        [JsMethod(Name = "startWifiScan")]
        public void StartWifiScan()
        { }
        #endregion

        #region Properties
        /// <summary>
        /// Returns the current network status.
        /// </summary>
        [JsProperty(Name = "status", NativeField = true)]
        public NetworkSignal Status { get { return null; } }
        #endregion

        #region Triggers (Events)
        /// <summary>
        /// trigger is received when a 3G/mobile connection is terminated. Additional trigger data is available in the NetworkSignal parameter passed to the callback function.
        /// </summary>
        public event Action<NetworkSignal> Mobile3GOff
        {
            [JsMethod(Name = "on", InsertArg0 = "'3gOff'")]
            add
            {
            }
            [JsMethod(Name = "removeListener", InsertArg0 = "'3gOff'")]
            remove
            {
            }
        }

        /// <summary>
        /// trigger is received when a device connects to a 3G (or other mobile) network and a WiFi connection is unavailable). Additional trigger data is available in the NetworkSignal parameter passed to the callback function.
        /// </summary>
        public event Action<NetworkSignal> Mobile3GOn
        {
            [JsMethod(Name = "on", InsertArg0 = "'3gOn'")]
            add
            {
            }
            [JsMethod(Name = "removeListener", InsertArg0 = "'3gOn'")]
            remove
            {
            }
        }

        /// <summary>
        /// trigger is received when a change in device's connectivity (3G / WiFi) is detected. Additional trigger data is available in the NetworkSignal parameter passed to the callback function.
        /// </summary>
        public event Action<NetworkSignal> Updated
        {
            [JsMethod(Name = "on", InsertArg0 = "'updated'")]
            add
            {
            }
            [JsMethod(Name = "removeListener", InsertArg0 = "'updated'")]
            remove
            {
            }
        }

        /// <summary>
        /// trigger is received when a WiFi connection is terminated. Additional trigger data is available in the NetworkSignal parameter passed to the callback function.
        /// </summary>
        public event Action<NetworkSignal> WifiOff
        {
            [JsMethod(Name = "on", InsertArg0 = "'wifiOff'")]
            add
            {
            }
            [JsMethod(Name = "removeListener", InsertArg0 = "'wifiOff'")]
            remove
            {
            }
        }

        /// <summary>
        /// trigger is received when a device connects to a WiFi network. Additional trigger data is available in the NetworkSignal parameter passed to the callback function.
        /// </summary>
        public event Action<NetworkSignal> WifiOn
        {
            [JsMethod(Name = "on", InsertArg0 = "'wifiOn'")]
            add
            {
            }
            [JsMethod(Name = "removeListener", InsertArg0 = "'wifiOn'")]
            remove
            {
            }
        }

        /// <summary>
        /// trigger is received on passive scan results of the OS and when scan is actively called. Additional trigger data is available in the WifiScanSignal parameter passed to the callback function.
        /// </summary>
        public event Action<WifiScanSignal> WifiScan
        {
            [JsMethod(Name = "on", InsertArg0 = "'wifiScan'")]
            add
            {
            }
            [JsMethod(Name = "removeListener", InsertArg0 = "'wifiScan'")]
            remove
            {
            }
        }
        #endregion
    }

    /// <summary>
    /// Holds the state of the network connectivity (3G, WiFi). NetworkSignal is fired on WiFi / 3G connectivity changes.
    /// </summary>
    [JsType(Mode = JsMode.Json, Export = false)]
    public class NetworkSignal : ScriptSignal
    {
        /// <summary>
        /// 
        /// </summary>
        [JsProperty(Name = "is3GOn", NativeField = true)]
        public bool Is3GOn { get { return false; } }

        /// <summary>
        /// 
        /// </summary>
        [JsProperty(Name = "isWifiOn", NativeField = true)]
        public bool IsWifiOn { get { return false; } }
    }

    /// <summary>
    /// A signal class for the WiFi scan results. Will be triggered on passive scan results of the OS and when scan is proactively called.
    /// </summary>
    [JsType(Mode = JsMode.Json, Export = false)]
    public class WifiScanSignal : ScriptSignal
    {
        /// <summary>
        /// Retrieves the list of results from the WiFi scan associated with this signal 
        /// Each ScanResult is an object with the fields: 
        /// String - BSSID - The address of the access point. 
        /// String - SSID - The network name.
        /// String - capabilities - Describes the authentication, key management, and encryption schemes supported by the access point. 
        /// Number - frequency - The frequency in MHz of the channel over which the client is communicating with the access point. 
        /// Number - level - The detected signal level in dBm.
        /// </summary>
        [JsProperty(NativeField = true, Name = "scanResults")]
        public ScanResult[] ScanResults { get { return null; } }
    }

    /// <summary>
    /// An  object with the fields: 
    /// String - BSSID - The address of the access point. 
    /// String - SSID - The network name.
    /// String - capabilities - Describes the authentication, key management, and encryption schemes supported by the access point. 
    /// Number - frequency - The frequency in MHz of the channel over which the client is communicating with the access point. 
    /// Number - level - The detected signal level in dBm.
    /// </summary>
    [JsType(Mode = JsMode.Prototype, Export = false)]
    public class ScanResult
    {
        //No way to new this up
        private ScanResult()
        { }

        /// <summary>
        /// The address of the access point. 
        /// </summary>
        [JsProperty(Name = "BSSID", NativeField = true)]
        public string BSSID { get { return null; } }

        /// <summary>
        /// The network name.
        /// </summary>
        [JsProperty(Name = "SSID", NativeField = true)]
        public string SSID { get { return null; } }

        /// <summary>
        /// Describes the authentication, key management, and encryption schemes supported by the access point.
        /// </summary>
        [JsProperty(Name = "capabilities", NativeField = true)]
        public string Capabilities { get { return null; } }

        /// <summary>
        /// The frequency in MHz of the channel over which the client is communicating with the access point.
        /// </summary>
        [JsProperty(Name = "frequency", NativeField = true)]
        public JsNumber Frequency { get { return null; } }

        /// <summary>
        /// The detected signal level in dBm.
        /// </summary>
        [JsProperty(Name = "level", NativeField = true)]
        public JsNumber Level { get { return null; } }
    }
}